from odoo import fields, models, api, _
from odoo.exceptions import UserError, ValidationError


class report_report_wtt_outbound_order(models.AbstractModel):
    _name = 'report.sb_stock_inherit.report_warehouse_entry_order'
    _description = 'WTT Warehouse Entry Report'

    @api.model
    def _get_report_values(self, docids, data=None):
        report = self.env['ir.actions.report']._get_report_from_name(
            'sb_stock_inherit.report_warehouse_entry_order')

        docs = self.env['stock.picking'].browse(docids)

        f_id = self.env['purchase.order'].sudo().search([('name', '=', docs[0].origin)], limit=1)
        f_p_id = f_id.partner_id
        for doc in docs:
            if doc.state in ('draft', 'cancel'):
                raise UserError(_('You cannot print a cancelled or draft picking.'))
            # if doc.picking_type_id.with_context(lang='zh_CN').name not in ['收货单']:
            #     raise UserError(_('Please select the Entry Order.'))
            if doc.origin == '':
                raise UserError(_('Please select the Entry Order.'))
            else:
                p_id = self.env['purchase.order'].sudo().search([('name', '=', doc.origin)], limit=1).partner_id.id
                if f_p_id.id != p_id:
                    raise UserError(_('Please select the purchase receipt from the same supplier.'))
        move = docs.move_ids_without_package.filtered(lambda x: x.product_id.detailed_type == 'product')
        flag = False
        for rec in move:
            categ_id = rec.product_id.get_top_category(rec.product_id.categ_id)
            if categ_id.code in ['BS', 'DF']:
                flag = True
        combined_values = []
        if len(docs) > 1:
            for doc in docs:
                for value in doc.move_ids_without_package:
                    combined_values.append(value)
        return {
            'doc_model': report.model,
            'docs': docs[0],
            'data': data,
            'flag': flag,
            'combined_values': combined_values if len(docs) > 1 else docs.move_ids_without_package,
            'po_id': f_id,
        }
